# coding:utf-8
from PyQt6.QtCore import QSize, Qt
from PyQt6.QtWidgets import QButtonGroup, QPushButton, QApplication, QHBoxLayout
from qfluentwidgets import BodyLabel, setFont, InfoBar, TransparentToolButton, FluentIcon as FIF, TransparentPushButton

from common import FakerMethod, FakerUtils, SingleIcon, Icon
from components import BaseScrollArea, TransparentDirectionPushButton
from .faker_views import BatchWindow, fakerSignals


class FakerView(BaseScrollArea):
    title = 'Faker 随机数据生成器'
    description = '使用Faker库生成随机数据'
    icon = SingleIcon.FALSE
    object_name = 'FakerView'

    def __init__(self, parent=None):
        super().__init__(parent)
        self.batchWindow = None
        self.fakerUtils = FakerUtils()
        self.titleLabel = BodyLabel(self.title, self)
        self.buttonGroup = QButtonGroup(self)

        self.horizontalLayout = QHBoxLayout(self)
        self.batchGenerationBtn = TransparentPushButton(Icon.BATCH, '批量生成', self)
        self.settingsBtn = TransparentToolButton(FIF.SETTING, self)

        self.__initWidgets()

    def __initWidgets(self):
        self.setReturnVisible(True)
        setFont(self.titleLabel, 24)

        self.horizontalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.horizontalLayout.setContentsMargins(36, 20, 36, 10)
        self.horizontalLayout.addWidget(self.returnBtn)
        self.horizontalLayout.addStretch(1)
        self.horizontalLayout.addWidget(self.batchGenerationBtn)
        self.horizontalLayout.addWidget(self.settingsBtn)

        self.scrollLayout.addWidget(self.titleLabel)

        self.init_providers()
        self.connectSignals()

    def init_providers(self):
        for button in self.buttonGroup.buttons():
            self.buttonGroup.removeButton(button)
            self.scrollLayout.removeWidget(button)
            button.deleteLater()

        provider: dict
        for provider in self.fakerUtils.get_providers():
            method = self.fakerUtils._str_to_method(provider['method'])
            method.set_kwargs(provider.get('kwargs', {}))

            button = TransparentDirectionPushButton(SingleIcon.FALSE.icon(), '   ' + provider['name'], self)
            button.setFixedHeight(60)
            button.setIconSize(QSize(40, 40))
            button.setAlignment(Qt.AlignmentFlag.AlignLeft)
            button.setProperty('method', method)
            setFont(button, 16)
            self.buttonGroup.addButton(button)
            self.scrollLayout.addWidget(button)

    def connectSignals(self):
        self.buttonGroup.buttonClicked.connect(self.onButtonClicked)
        self.batchGenerationBtn.clicked.connect(self.onBatchGenerationClicked)
        fakerSignals.addProvider.connect(self.init_providers)
        fakerSignals.removeProvider.connect(self.init_providers)

    def onButtonClicked(self, button: QPushButton):
        method: FakerMethod = button.property('method')
        data = self.fakerUtils.run(method, **method.kwargs())
        QApplication.clipboard().setText(data)
        InfoBar.success('', '已复制到剪贴板', parent=self)

    def onBatchGenerationClicked(self):
        def onBatchWindowClosed():
            self.batchWindow = None

        if self.batchWindow:
            self.batchWindow.activateWindow()
            self.batchWindow.show()
            return
        self.batchWindow = BatchWindow()
        self.batchWindow.closed.connect(onBatchWindowClosed)
        self.batchWindow.show()
